package hedron.db.model;

import hedron.Application;
import hedron.db.DBManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Date;

public class ContactComment extends AbstractModel {

	private Date C_DATE;
	private long CUSTOMER_ID;
	private long USER_ID;
	public String U_FULLNAME;
	public String C_COMMENT = EMPTY_STR;

	@Override
	public long getID() {
		return C_DATE.getTime();
	}

	@Override
	public String getIDName() {
		return "C_DATE";
	}

	@Override
	public String getTableName() {
		return "HCOMMMENT";
	}

	public String getDateString() {
		return Application.formatDate(C_DATE);
	}

	void init() {
		C_DATE = new Date();
	}

	public ContactComment(long customerId) {
		C_DATE = new Date();
		CUSTOMER_ID = customerId;
		USER_ID = Application.getCurrentUserID();
	}

	public ContactComment(ResultSet rs) {
		if (rs == null) {
			throw new IllegalArgumentException("ResultSet might not be null");
		}
		if (!load(rs)) {
			throw new IllegalArgumentException("cannot load from ResultSet");
		}
	}

	@Override
	public boolean load(ResultSet rs) {
		DBManager db = Application.DB;
		C_DATE = db.getDate(rs, "C_DATE");
		if (C_DATE == null) {
			return false;
		}
		CUSTOMER_ID = db.getLong(rs, "CUSTOMER_ID");
		USER_ID = db.getLong(rs, "USER_ID");
		C_COMMENT = db.getString(rs, "C_COMMENT");
		U_FULLNAME = db.getString(rs, "U_FULLNAME");
		return true;
	}

	@Override
	public boolean isValid() {
		return C_DATE != null && isNotEmpty(C_COMMENT);
	}

	@Override
	public boolean store(Connection conn) {
		StringBuilder sql = new StringBuilder();
		sql.append("insert into ");
		sql.append(getTableName());
		sql.append("(C_DATE, CUSTOMER_ID, USER_ID, C_COMMENT) values(?,?,?,?)");
		return db.ddlQuery(conn, sql.toString(), C_DATE, CUSTOMER_ID, USER_ID, C_COMMENT);
	}

	@Override
	public boolean delete(Connection conn) {
		return false;
	}

}
